package com.qf.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.blog.dto.Email;
import com.qf.blog.entity.User;
import com.qf.blog.exception.BlogException;
import com.qf.blog.mapper.UserMapper;
import com.qf.blog.service.IUserService;
import com.qf.blog.utils.EmailUtils;
import com.qf.blog.utils.UUIDUtils;
import com.qf.blog.vo.UserVo;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 达哥
 * @since 2023-03-21
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private EmailUtils emailUtils;

    /**
     * 实现注册的业务
     *
     * @param userVo
     */
    @Override
    public void register(UserVo userVo) {

        // 1.先判断这个邮箱是否已经被注册了
        User dbUser = getUserByEmail(userVo.getEmail());

        if (dbUser != null) {
            throw new BlogException(1001, "邮箱已被注册");
        }

        // 判断用户名是否被注册
        dbUser = getUserByUsername(userVo.getUsername());
        if (dbUser != null) {
            throw new BlogException(1001, "用户名已经被注册");
        }

        // 2.密码加密
        User user = new User();
        user.setUsername(userVo.getUsername());
        user.setPassword(BCrypt.hashpw(userVo.getPwd(), BCrypt.gensalt()));
        user.setEmail(userVo.getEmail());
        user.setStatus(0); // 未激活
        user.setActivationCode(UUIDUtils.getId()); // 设置激活码?激活的时候使用
        user.setCreateTime(new Date()); // 当前时间

        // 3.新用户入库
        int insert = baseMapper.insert(user);

        // 4.给用户的邮箱发送激活连接
        if (insert > 0) {
            String url = "http://127.0.0.1:8080/blog/user/activation?code=" + user.getActivationCode(); // 因为这个连接在邮箱中点击的，所以不要写localhost
            Email email = new Email();
            email.setTo("854569279@qq.com");
            email.setTitle("千锋Java学科博客账号激活");
            email.setContent("新用户注册成功，需要激活后才能使用，点击<a href = '" + url + "'>这里</a>激活");
            email.setCc("1368996491@qq.com");
            // 发送邮件这个方法耗时最长，使用启动一个线程异步来调用
            new Thread(() -> {
                emailUtils.sendEmail(email);
            }).start();
        }
    }

    @Override
    public User getUserByEmail(String email) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email);
        return userMapper.selectOne(queryWrapper);  // select * from t_user where email = ?
    }

    @Override
    public Integer updateUserStatus(String code) {
        return userMapper.updateUserStatus(code);
    }

    @Override
    public User getUserByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return userMapper.selectOne(queryWrapper); // select * from t_user where username = ?
    }

    @Override
    public Integer updateUserHeader(Integer id, String fileUrl) {
        return userMapper.updateUserHeader(id,fileUrl);
    }
}
